package com.egao.common.publicly;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.exception.BusinessException;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.module.counsel.CounselConstants;
import com.egao.common.module.counsel.entity.Counsel;
import com.egao.common.module.counsel.entity.CounselStudent;
import com.egao.common.module.counsel.entity.CounselTeacher;
import com.egao.common.module.counsel.service.CounselService;
import com.egao.common.module.counsel.service.CounselStudentService;
import com.egao.common.module.counsel.service.CounselTeacherService;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.student.service.StudentService;
import com.egao.common.module.system.entity.User;
import com.egao.common.module.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author lym
 * @date 2022/2/16 10:31
 */
@Api(tags = "预约咨询")
@RestController
@RequestMapping("${api}")
public class PubliclyCounselController extends BaseController {

    @Autowired
    private CounselService counselService;
    @Autowired
    private CounselTeacherService counselTeacherService;
    @Autowired
    private UserService userService;
    @Autowired
    private CounselStudentService counselStudentService;
    @Autowired
    private StudentService studentService;



    @ApiOperation("预约咨询日历")
    @GetMapping("/publicly/counsel/calendar")
    public JsonResult calendar(String date){
        return JsonResult.ok().setData(counselService.calendar(date, getLoginUser()));
    }

    @ApiOperation("预约咨询详情")
    @GetMapping("/publicly/counsel/detail/{id}")
    public JsonResult calendar(@PathVariable("id") Integer id){
        Counsel counsel = counselService.getById(id);
        if (counsel == null) {
            return JsonResult.error("参数异常");
        }
        if (System.currentTimeMillis() >= counsel.getCounselDate().getTime()) {
            return JsonResult.error("预约时间已过期");
        }

        CounselStudent old = counselStudentService.getOne(new LambdaQueryWrapper<CounselStudent>()
                .eq(CounselStudent::getCounselId, id)
                .ne(CounselStudent::getState, CounselConstants.STUDENT_STATE_CANCEL)
                .last("limit 1")
        );
        if (old != null) {
            throw new BusinessException("该咨询已被预约");
        }

        CounselTeacher counselTeacher = counselTeacherService.getById(counsel.getCounselTeacherId());
        User user = userService.getById(counselTeacher.getUserId());

        counsel.setTeacherName(user.getName());
        counsel.setTeacherContent(counselTeacher.getContent());
        return JsonResult.ok().setData(counsel);
    }

    @PreAuthorize("hasAuthority('student')")
    @ApiOperation(value = "申请预约咨询")
    @OperLog(value = "申请预约咨询", desc = "申请预约咨询", result = true)
    @PostMapping("/publicly/counsel/save")
    public JsonResult save(@RequestBody CounselStudent counselStudent) {
        if (getLoginUser() == null) {
            return JsonResult.error("请先登录");
        }

        Student student = studentService.getByUserId(getLoginUserId());
        if (student == null) {
            return JsonResult.error("没有找到学生信息");
        }
        counselStudent.setStudentId(student.getId());
        return counselStudentService.saveCounselStudent(counselStudent);
    }
}
